<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Sms extends Base
{
    //菜单列表
    public function index()
    {
        $data = $this->request->param();
        if (isset($data['ajax']) && $data['ajax'] == 1) {
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if (isset($data['search'])) {
                $list = Db::name('company')->where('name', 'like', '%' . $data['search'] . '%')->order('id desc')->limit($start, $limit)->select();
                $count = Db::name('company')->where('name', 'like', '%' . $data['search'] . '%')->count();
            } else {
                $list = Db::name('company')->order('id desc')->limit($start, $limit)->select();
                $count = Db::name('company')->count();
            }

            return $this->json_data($count, $list);
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        if (empty($id)) {
            $info = [];
        } else {
            $info = Db::name('company')->where('id', $id)->find();
        }
        $this->assign('info', $info);
        return $this->fetch('edit');
    }
    //编辑栏目1-2
    public function save()
    {
        $status = 0;
        $msg = "操作失败";
        $data = $this->request->param();

        if (empty($data['id'])) {
            //执行操作
            $res = Db::table('company')->insert($data);

            if ($res) {
                $status = 1;
                $msg = "添加成功";
                $cname = '添加' . $data['name'];
                $ctype = '添加';
            }
        } else {
            $id = $data['id'];
            //执行更新操作
            $res = Db::table('company')->where('id', $id)->update($data);

            if ($res) {
                $status = 1;
                $msg = "更新成功";
                $cname = '更新' . $data['name'];
                $ctype = '更新';
            }
        }
        adminlog($cname, $ctype);
        return ['status' => $status, 'msg' => $msg];
    }

    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        if (empty($id)) {
            $msg .= '，ID不存在';
        } else {
            $brand = Db::name('brand')->where('company_id', $id)->find();
            if ($brand) {
                return ['status' => 0, 'msg' => '删除失败，其名下存在品牌，请先删除品牌'];
            }
            $del = Db::name('company')->where('id', $id)->delete();
            if ($del) {
                $status = 1;
                $msg = "删除成功";
                adminlog('删除id:' . $id, '删除');
            }
        }
        return ['status' => $status, 'msg' => $msg];
    }

    // 导出功能
    public function daochu()
    {
        $data = $this->request->param();
        if (isset($data['search'])) {
            $lists = Db::name('company')->where('name', 'like', '%' . $data['search'] . '%')->order('id desc')->select();
        } else {
            $lists = Db::name('company')->order('id desc')->select();
        }

        $spreadsheet = new Spreadsheet();
        $PHPSheet = $spreadsheet->getActiveSheet();

        $PHPSheet->setTitle("短信配置"); //给当前活动sheet设置名称
        $PHPSheet->setCellValue("A1", "id")
            ->setCellValue("B1", "api名称")
            ->setCellValue("C1", "api密码")
            ->setCellValue("D1", "企业")
            ->setCellValue("E1", "备注");

        $styleArray = [
            'alignment' => [
                'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
            ],
        ];
        foreach ($lists as $key => $val) {
            $spreadsheet->setActiveSheetIndex(0)
                ->setCellValue("A" . ($key + 2), $val['id'])
                ->setCellValue("B" . ($key + 2), $val['api_name'])
                ->setCellValue("C" . ($key + 2), $val['api_pass'])
                ->setCellValue("D" . ($key + 2), $val['company'])
                ->setCellValue("E" . ($key + 2), $val['mark']);
            $spreadsheet->setActiveSheetIndex(0)->getStyle('A' . ($key + 2))->applyFromArray($styleArray);
        }

        $spreadsheet->getActiveSheet()->getColumnDimension('B')->setWidth(30);

        $time = date('Y-m-d', time());
        // Redirect output to a client’s web browser (Xlsx)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="短信配置' . $time . '.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
        // If you're serving to IE over SSL, then the following may be needed
        header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0

        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
        $writer->save('php://output');
    }
}
